package com.gitee.kamismile.gatewayAgent.server.bootstrap;

import com.gitee.kamismile.gatewayAgent.server.handler.GateWayServerInitializer;
import com.gitee.kamismile.stone.commmon.util.SnowflakeIdWorker;
import com.gitee.kamismile.stone.commmon.util.ValueUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GatewayAgentConfiguration {

    @Autowired
    GatewayAgentConstant gatewayAgentConstant;

    @Bean
    public SnowflakeIdWorker createId() {
        return new SnowflakeIdWorker(ValueUtils.isLongNull(gatewayAgentConstant.getRouter()), ValueUtils.isLongNull(gatewayAgentConstant.getRouter().subSequence(0, 1)));
    }


    @Bean
    @ConditionalOnMissingBean(GateWayServerInitializer.class)
    public GateWayServerInitializer gateWayServerInitializer() {
        GateWayServerInitializer serverInitializer = new GateWayServerInitializer();
        serverInitializer.setSnowflakeIdWorker(createId());
        serverInitializer.setGatewayAgentConstant(gatewayAgentConstant);
        return serverInitializer;
    }

//    @Bean
//    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
//    @ConditionalOnMissingBean(GateWayServerHandler.class)
//    public GateWayServerHandler gateWayServerHandler() {
//        GateWayServerHandler gateWayServerHandler = new GateWayServerHandler();
//        gateWayServerHandler.setSnowflakeIdWorker(createId());
//        gateWayServerHandler.setGatewayAgentConstant(gatewayAgentConstant);
//        return gateWayServerHandler;
//    }
}
